﻿using Microsoft.EntityFrameworkCore;
using PlatformUW.Contracts;
using PlatformUW.EntityFramework;

namespace PlatformUW.API.Extensions
{
    public static class ServiceExtensions
    {
        /// <summary>
        /// 跨域配置函数
        /// </summary>
        /// <param name="services"></param>
        public static void ConfigureCors(this IServiceCollection services)
        {
            services.AddCors(options =>
            {
                options.AddPolicy("CorsPolicy",
                    builder => builder.AllowAnyOrigin()
                                      .AllowAnyMethod()
                                      .AllowAnyHeader());
            });
        }

        /// <summary>
        /// MySQL数据库上下文配置函数
        /// </summary>
        /// <param name="services"></param>
        /// <param name="config"></param>
        public static void ConfigurationMySqlContext(this IServiceCollection services, IConfiguration config)
        {
            var connectionString = config.GetConnectionString("PlatformDb");
            services.AddDbContext<PlatformDBContext>(builder =>
            {
                builder.UseMySql(connectionString, MySqlServerVersion.LatestSupportedServerVersion,
                    options => options.EnableRetryOnFailure());
            });
        }

        /// <summary>
        /// 注册仓储模式的服务
        /// </summary>
        /// <param name="services"></param>
        public static void ConfigureRepositoryWrapper(this IServiceCollection services)
        {
            services.AddScoped<IRepositoryWrapper, RepositoryWrapper>();            
        }
    }
}
